if (!ubi) ubi = {}
ubi.notebook_view = {
init: 
    function () {
        ubi.notebook_view.update_note_list();
        ubi.notebook_view.bind_add_btn_click();
    },
add_note:
    function () {
        var title = prompt('Create a new Note:');
        if (title == null) return;
        add_note_do(title,
            function (state, data) {
            var note = data
            ubi.notebook_view.update_note_list();
            notify('Add notebook `'
                + note.title +'` done.');
            },
            function (state, data){
            notify('Add notebook failed.');
            });
 
    },

open_note:
    function (note_id, block_id, background) {
        if (!global.open_notes.hasOwnProperty(note_id))  {
            global.open_notes[note_id] = new note_view(note_id)
            global.open_notes[note_id].open(background) //open and reload
        } 
        global.crr_note_id = note_id;
        if (block_id) {
            global.open_notes[note_id].locate_anchor(block_id);
        }
        if (background != true) {
            ubi.tabs.select(global.open_notes[note_id].div);        
        }
    },

rename_note:
    function (id, new_title) {
        rename_note_do(
            id, new_title,
            function () {
                $('#'+id+'_note-item')
                    .find('#note-item-title-replacement')
                        .replaceWith(new_title);             
            },
            function () {
                notify('Rename Note Error.')
            }
            )
    },

update_note_list: 
    function  () {
        get_note_list_do(
        function (state, data) {
            var notes = data;
            var notes_array = new Array();
            for (var i = 0; i < notes.length; i += 1) {   
                notes_array.push(
                    form_note(notes[i].id, notes[i].title));
            }
            $('#notebook-view-content').html(notes_array.join(''));
            for (var i = 0; i < notes.length; i += 1) {   
                $('#'+ notes[i].id+'_note-item').qtip({
                    content: notes[i].title,
                    position: {
                      corner: {
                        tooltip: 'topMiddle',
                         target: 'bottomMiddle',
                      }
                   }
                });
            }
            //bind actions
            ubi.notebook_view.bind_remove_btn_click();
            ubi.notebook_view.bind_note_item_icon_click();
            ubi.notebook_view.bind_note_item_title_dblclick();
            ubi.notebook_view.bind_note_item_hover();
        },
        function (state, data){
            notify('Load Notebook fail.');
        });
    },

bind_note_item_title_dblclick:
    function () {
        $('.note-item-title').unbind();
        $('.note-item-title').dblclick(function () {
            var old = $(this).text();
            $(this).html('<textarea id="note-item-title-replacement">'+old+'</textarea>');
            $(this).find('#note-item-title-replacement').click(
                function (){
                    return false;
                });
        });
    },

bind_note_item_hover:
    function () {
        $('.note-item').hover(function () {
            $(this).find('.note-item-btns').show();
        },
        function () {
            $(this).find('.note-item-btns').hide();
        });
    },

bind_remove_btn_click:
    function () {
        $('#notebook-view .note-item-remove-btn').unbind();
        $('#notebook-view .note-item-remove-btn').click(function (){
            var note_id = $($(this).parents('.note-item')[0]).attr('id').toString().split('_')[0];
            var title = $('#'+note_id+'_note-item').find('.note-item-title').text()
            if(confirm('Are you sure you want to permanently delete `'
                    + title + '` ?'))
            {
                remove_note_do(note_id, 
                function (state, data) {
                    ubi.notebook_view.update_note_list();
                    if (global.crr_note_id == data['id']){
                        //switch_page(0);
                        global.crr_note_id = -1;
                    }
                    notify('Remove notebook `'
                        + data['title'] + '` done. You can not Undo yet.');
                },
                function (state, data) {
                    notify('Remove notebook fail.');    
                });
            }
            return false;
        }); 
    },

bind_add_btn_click:
    function () {
        $('#note-new-btn').click(function (){
            ubi.notebook_view.add_note();
        });
    },


bind_note_item_icon_click:
    function () {
        $('.note-item-icon-wrap').unbind();    
        $('.note-item-icon-wrap').click(function (event) {
            var note_item = $(this).parents('.note-item');
            var note_id = note_item.attr('id').toString().split('_')[0];
            if (event.ctrlKey) // open in background
                ubi.notebook_view.open_note(note_id, null, true)
            else  
                ubi.notebook_view.open_note(note_id);
        });
    },

apply_filter:
    function (filter_text) {
        $('#notebook-view .note-item').each(function () {
            if ($(this).find('.note-item-title').text().search(filter_text) == -1) {
                $(this).fadeOut();
            } else {
                $(this).fadeIn();
            }
        });
    }
}
